<template>
  <md-page
    :title="data.info?.point_name || '扫码确认信息'"
    backUrl="/pages/my/index"
    isBtn
    :btnTextItems="data.btnTextItems"
    @btnClick="bottomBtnClick">
    <div class="container">
      <div class="title">{{ data.info?.team_name || '' }}</div>
      <div class="content">
        <div class="card" v-if="data.info?.main_user">
          <div class="item">
            <div>姓名/年龄</div>
            <div class="itemCard">
              <div class="name">{{ data.info?.main_user?.truename || '' }}</div>
              <div class="age">{{ data.info?.main_user?.age ? data.info?.main_user?.age + '岁' : '' }}</div>
            </div>
          </div>
          <div class="item">
            <div>证件正面</div>
            <div class="itemCard" @click="() => handlePreview(data.info?.main_user?.front_img)">
              <md-icon :url="data.info?.main_user?.front_img" width="147" height="100" />
            </div>
          </div>
          <div class="item">
            <div>证件背面</div>
            <div class="itemCard" @click="() => handlePreview(data.info?.main_user?.back_img)">
              <md-icon :url="data.info?.main_user?.back_img" width="147" height="100" />
            </div>
          </div>
        </div>
        <div class="card grey m-top-20" v-if="data.info?.sub_user_list?.length > 0">
          <div class="flex" v-for="item in data.info?.sub_user_list" :key="item.id">
            <div class="item">
              <div>姓名/年龄</div>
              <div class="itemCard">
                <div class="name">{{ item?.truename || '' }}</div>
                <div class="age">{{ item?.age ? item?.age + '岁' : '' }}</div>
              </div>
            </div>
            <div class="item">
              <div>证件正面</div>
              <div class="itemCard" @click="() => handlePreview(item?.front_img)">
                <md-icon :url="item?.front_img" width="147" height="100" />
              </div>
            </div>
            <div class="item">
              <div>证件背面</div>
              <div class="itemCard" @click="() => handlePreview(item?.front_img)">
                <md-icon :url="item?.back_img" width="147" height="100" />
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="title title-rate flex-c m-top-20">
        <div class="flex-l" v-if="data.query?.type == '1'">
          完成率：
          <div class="rate">{{ data.info?.percent || 0 }}%</div>
        </div>
        <div class="flex-l" v-else-if="data.query?.type == '2'">
          发放人数：
          <div class="rate">{{ data.info?.send_num || 0 }}</div>
        </div>
      </div>
      <div class="collapse-wrap m-top-20">
        <block v-if="data.query?.type == '1'">
          <div class="title flex-b">
            总打卡点数
            <span>{{ data.info?.all_check_num || 0 }}</span>
          </div>
          <div class="title flex-b checked">
            已打卡点数
            <span>{{ data.info?.checkInNum || 0 }}</span>
          </div>
          <div class="title flex-b not">
            未打卡点位
            <span>{{ data.info?.NoCheckInNum || 0 }}</span>
          </div>
          <view class="content" v-if="data.info?.NoCheckInList?.length > 0">
            <view class="item" v-for="item in data.info?.NoCheckInList" :key="item.id">{{ item.point_name }}</view>
          </view>
        </block>
        <div class="title flex-b" v-else-if="data.query?.type == '2' && data.info?.remark">
          {{ data.info?.remark || '' }}
        </div>
      </div>
    </div>
  </md-page>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import type { BtnTextItem } from '@/components/md-ui/components/md-page/md-page.vue';
// 接口
import { matchScanCodeDetail, matchScanCodeDetailSubmit, matchScanCodeSupplySubmit } from '@/api';
import type { Match } from '@/api/data';

const data = reactive<any>({
  query: {}, // 页面传参
  btnTextItems: [
    { text: '取消', key: 'cancel' },
    { text: '通过审核', key: 'ok' },
  ],
  info: {},
});

// 图片预览
const handlePreview = (url: string) => {
  if (!url) return;
  uni.previewImage({
    current: url,
    urls: [url],
  });
};

const bottomBtnClick = async (info: { item: BtnTextItem }) => {
  const { key } = info.item;
  // 通过审核
  if (key === 'ok') {
    uni.showModal({
      title: '提示',
      content: '是否确认提交?',
      success: res => {
        if (res.confirm) {
          if (data.query?.type == '1') {
            fetchMatchScanCodeDetailSubmit();
          } else {
            fetchMatchScanCodeSupplySubmit();
          }
        }
      },
    });
  } else {
    //  取消
    uni.navigateBack();
  }
};

/**
 * 接口相关
 */
// 确认信息
const fetchMatchScanCodeDetail = async (query: Match.ScanCode.Detail.Body) => {
  try {
    const res = await matchScanCodeDetail(query);
    // console.log(res)
    let main_user = res.data?.user_list?.slice(0, 1)?.[0];
    let sub_user_list = res.data?.user_list?.slice(1);
    data.info = { ...res.data, main_user, sub_user_list };
    // 重复打卡点扫码
    if (data.info?.all_check_num) {
      let pName = data.info?.point_name || '';
      let noCheckList = data.info?.NoCheckInList || [];
      if (pName) {
        let flag = noCheckList.find((item: any) => item.point_name === pName);
        if (!flag) {
          uni.showModal({
            title: '提示',
            content: '当前点位已扫码',
            showCancel: false,
            success: res => {
              if (res.confirm) {
                // uni.navigateBack();
                // 扫码
                handleScanCode();
              }
            },
          });
        }
      }
    } else if (data.info?.send_num) {
      // 重复扫补给券
    }
  } catch (error) {
    console.log(error, 2);
  }
};

// 打卡信息确认提交
const fetchMatchScanCodeDetailSubmit = async () => {
  try {
    const res = await matchScanCodeDetailSubmit({ ...data.query, type: undefined });
    // Toast(res?.msg || '提交成功');
    uni.showModal({
      title: '提示',
      content: res?.msg || '提交成功',
      showCancel: false,
      success: res => {
        if (res.confirm) {
          // uni.navigateBack();
          // 扫码
          handleScanCode();
        }
      },
    });
  } catch (error) {}
};

// 补给信息确认提交
const fetchMatchScanCodeSupplySubmit = async () => {
  try {
    const res = await matchScanCodeSupplySubmit({ ...data.query, type: undefined });
    // Toast(res?.msg || '提交成功');
    uni.showModal({
      title: '提示',
      content: res?.msg || '提交成功',
      showCancel: false,
      success: res => {
        if (res.confirm) {
          // uni.navigateBack();
          // 扫码
          handleScanCode();
        }
      },
    });
  } catch (error) {}
};

const handleScanCode = () => {
  // 允许从相机和相册扫码
  uni.scanCode({
    success: function (res) {
      let url = res.result;
      if (url) {
        uni.navigateTo({ url });
      }
    },
  });
};

onLoad(option => {
  data.query = option;
  // console.log(option)
  fetchMatchScanCodeDetail(option as Match.ScanCode.Detail.Body);
});
</script>

<style lang="scss" scoped>
.container {
  padding: 30rpx;
  box-sizing: border-box;
  & > .title {
    width: 100%;
    height: 110rpx;
    line-height: 110rpx;
    color: white;
    border-radius: 30rpx 30rpx 0 0;
    background: $primary;
    font-size: 40rpx;
    text-align: center;
  }
  & > .content {
    padding: 20rpx;
    box-sizing: border-box;
    background: white;
    & > .card {
      background: #cbe8ee;
      display: flex;
      border-radius: 20rpx;
      padding: 23rpx 30rpx;
      box-sizing: border-box;
      gap: 18rpx;
      &.grey {
        background: #f7f7f7;
        display: flex;
        flex-direction: column;
      }
      .item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        color: $sub;
        .itemCard {
          width: 182rpx;
          height: 129rpx;
          background: white;
          border-radius: 10rpx;
          margin-top: 24rpx;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          .name {
            font-size: 32rpx;
            font-weight: bold;
            color: $primary;
          }
          .age {
            font-size: 26rpx;
            font-weight: 500;
          }
        }
      }
    }
  }
  & > .title-rate {
    background: linear-gradient(180deg, rgba($primary, 0.2) 0%, #fff 100%);
    border-radius: 10rpx;
    color: $title;
    .rate {
      color: $primary;
    }
  }
  .collapse-wrap {
    background: white;
    padding: 20rpx;
    .title {
      padding: 0 20rpx;
      box-sizing: border-box;
      height: 88rpx;
      line-height: 88rpx;
      font-size: 28rpx;
      font-weight: bold;
      margin-bottom: 20rpx;
      background: #f7f7f7;
      border-radius: 20rpx;
      &.checked {
        color: $primary;
      }
      &.not {
        color: $danger;
      }
    }
    .content {
      padding: 20rpx;
      box-sizing: border-box;
      background: #f7f7f7;
      border-radius: 20rpx;
      .item {
        width: 100%;
        height: 88rpx;
        line-height: 88rpx;
        text-align: center;
        background: white;
        border-radius: 20rpx;
        font-size: 28rpx;
        font-weight: bold;
        &:not(:last-of-type) {
          margin-bottom: 20rpx;
        }
      }
    }
  }
}
</style>
